# coding:utf-8
import time
import tkinter as tk

import ttkbootstrap as tkBoot
from ttkbootstrap import utility
from ttkbootstrap.constants import *

from ui.add_class_box import AddClassBox
from ui.student_tab import StudentTab
from util.mysql_util import get_classes, delete_classes
from util.ui_util import create_close_btn


class ClassTab(tkBoot.Frame):
    def __init__(self, notebook):
        super().__init__(notebook)
        self.notebook = notebook
        self.rowdata = get_classes(fetchNameProp=False)
        self.classTable = tkBoot.Treeview(
            master=self,
            bootstyle=tkBoot.INFO,
            columns=[0, 1, 2, 3],
            show=tkBoot.HEADINGS
        )

    def initFrame(self):
        create_close_btn(self, "classTab")

        btnFrame = tkBoot.Frame(self)
        add_button = tkBoot.Button(btnFrame, text="+", command=self.open_add_box, bootstyle="success")
        add_button.pack(side=tk.LEFT, padx=(0, 10))
        delete_button = tkBoot.Button(btnFrame, text=" - ", command=self.delete_row, bootstyle="danger")
        delete_button.pack(side=tk.LEFT, padx=(0, 10))
        config_button = tkBoot.Button(btnFrame, text=" ⩸ ", command=self.config_row, bootstyle="info")
        config_button.pack(side=tk.LEFT)
        btnFrame.pack(anchor=tk.NW, padx=(10, 100))

        tipLabel = tkBoot.Label(self, text="请不要随意删除数据！！！", bootstyle="danger")
        tipLabel.pack(side=tk.BOTTOM)

        self.create_table()
        self.refresh_table_data()
        self.classTable.bind("<ButtonRelease-3>", lambda event: self.config_row())

        self.notebook.add(self, text="新建班级")
        self.notebook.select(self)

    def create_table(self):
        self.classTable.heading(0, text='班级Id', anchor=tk.CENTER)
        self.classTable.heading(1, text='班级名称', anchor=tk.CENTER)
        self.classTable.heading(2, text='创建时间', anchor=tk.W)
        self.classTable.heading(3, text='更新时间', anchor=tk.W)
        self.classTable.column(
            column=0,
            anchor=tk.CENTER,
            width=utility.scale_size(self, 100),
            stretch=False
        )
        self.classTable.column(
            column=1,
            anchor=tk.W,
            width=utility.scale_size(self, 200),
            stretch=False
        )
        self.classTable.column(
            column=2,
            anchor=tk.W,
            width=utility.scale_size(self, 200),
            stretch=False
        )
        self.classTable.column(
            column=3,
            anchor=tk.W,
            width=utility.scale_size(self, 200),
            stretch=False
        )

        self.classTable.pack(fill=tk.BOTH, expand=tk.YES, pady=10)

    def refresh_table_data(self):
        cids = self.classTable.get_children()
        for cid in cids:
            self.classTable.delete(cid)
        data = get_classes(False)
        for row in data:
            self.classTable.insert(parent="", index=END, values=row)

    def open_add_box(self):
        addClassBox = AddClassBox(self)
        addClassBox.initFrame()

    def delete_row(self, cid=None):
        if cid is None:
            cid = self.classTable.selection()
        if cid is not None:
            item = self.classTable.item(cid, "values")
            self.classTable.delete(cid)
            delete_classes(item[0])
            time.sleep(0.1)

    def config_row(self):
        if len(self.classTable.selection()) == 1:
            cid = self.classTable.selection()
            item = self.classTable.item(cid, "values")
            studentTab = StudentTab(self.notebook, item)
            studentTab.initFrame()
